package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONObject;

import cn.taketoday.live.WebSocketMessage;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.config.settings.ThankGiftSetConf;
import cn.taketoday.live.data.Gift;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.data.superchat.SuperChat;
import cn.taketoday.live.tools.ParseIndentityTools;
import cn.taketoday.live.tools.ParseSetStatusTools;
import cn.taketoday.live.tools.ShieldGiftTools;
import cn.taketoday.live.utils.JodaTimeUtils;

/**
 * @author TODAY 2021/5/17 23:14
 */
public class SuperChatMessageCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws Exception {
    final JSONObject jsonObject = context.getOriginal();
    final StringBuilder stringBuilder = new StringBuilder(32);

    if (context.contains(ShieldMessage.is_gift)) {
      SuperChat superChat = JSONObject.parseObject(jsonObject.getString("data"), SuperChat.class);
      stringBuilder.append(JodaTimeUtils.formatDateTime(superChat.getStart_time() * 1000));
      stringBuilder.append(":收到留言:");
      stringBuilder.append(superChat.getUser_info().getUname());
      stringBuilder.append(" 他用了");
      stringBuilder.append(superChat.getPrice() * 1000);
      stringBuilder.append("金瓜子留言了");
      stringBuilder.append(ParseIndentityTools.parseTime(superChat.getTime()));
      stringBuilder.append("秒说: ");
      stringBuilder.append(superChat.getMessage());
      superChat.setTime(ParseIndentityTools.parseTime(superChat.getTime()));
      //控制台打印
      if (context.contains(ShieldMessage.is_cmd)) {
        System.out.println(stringBuilder);
      }
      context.sendMessage(WebSocketMessage.of("superchat", superChat));
      if (GlobalData.logThread != null && !GlobalData.logThread.FLAG) {
        GlobalData.logString.add(stringBuilder.toString());
        synchronized(GlobalData.logThread) {
          GlobalData.logThread.notify();
        }
      }

      stringBuilder.delete(0, stringBuilder.length());
    }
    if (context.contains(ShieldMessage.is_giftThank)) {
      if (GlobalData.parsethankGiftThread != null && !GlobalData.parsethankGiftThread.TFLAG) {
        SuperChat superChat = JSONObject.parseObject(jsonObject.getString("data"), SuperChat.class);
        Gift gift = new Gift();
        stringBuilder.append(ParseIndentityTools.parseTime(superChat.getTime()));
        stringBuilder.append("秒");
        stringBuilder.append(superChat.getGift().getGift_name());
        gift.setGiftName(stringBuilder.toString());
        gift.setNum(superChat.getGift().getNum());
        gift.setPrice(superChat.getPrice() * 1000);
        gift.setTotal_coin((long) superChat.getPrice() * 1000l);
        gift.setTimestamp(superChat.getStart_time() * 1000);
        gift.setAction("赠送");
        gift.setCoin_type((short) 1);
        gift.setUname(superChat.getUser_info().getUname());
        gift.setUid(superChat.getUid());

        final MessageHandler messageThread = context.getMessageHandler();

        final ThankGiftSetConf thankGiftSetConf = messageThread.getThankGiftSetConf();
        gift = ShieldGiftTools.shieldGift(
                gift,
                ParseSetStatusTools.getGiftShieldStatus(thankGiftSetConf.getShield_status()),
                thankGiftSetConf.getGiftStrings(), null
        );
        if (gift != null) {
          messageThread.parseGiftSetting(gift);
        }
      }
    }
//					LOGGER.debug("收到醒目留言:::" + message);
  }

}
